<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/9/29
 * Time: 18:40
 */
namespace app\order\model;

use think\Db;
class ModelSupplies extends BaseModel{
    protected $tableName = 'ari_model_supply';

    public function page($field = '*', $where = '', $order = ''){
        $num = 10;
        $join = array(
            ['ari_model_types model', $this->tableName.'.modelid=model.model_id'],
            ['ari_supply supply', $this->tableName.'.supid=supply.sup_id'],
            ['ari_orders_exec exec', $this->tableName.'.oidexec=exec.oid_exec'],
            ['ari_orders ord', 'ari_orders_exec.oid=ord.oid'],
            ['ari_clients cli', 'ari_orders.cid=cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        $saleId = session('order_saleid');
        if($saleId != 0){
            $where['ari_orders.sale_id'] = $saleId;
        }
        $count = Db::table($this->tableName)->field($field)->where($where)
            ->join($join)->count();
        $data = Db::table($this->tableName)->field($field)->where($where)->order($order)
            ->join($join)
            ->paginate($num, $count, ['query' => request()->param()]);
        return $data;
    }

    public function find($where){
        $data = Db::table($this->tableName)->where($where)
            ->join('ari_model_types model', $this->tableName.'.modelid=model.model_id')
            ->join('ari_supply supply', $this->tableName.'.supid=supply.sup_id')
            ->join('ari_orders_exec exec', $this->tableName.'.oidexec=exec.oid_exec')->find();
        return $data;
    }
    public function search($where){
        $num = 10;
        $join = array(
            ['ari_model_types model', $this->tableName.'.modelid=model.model_id'],
            ['ari_supply supply', $this->tableName.'.supid=supply.sup_id'],
            ['ari_orders_exec exec', $this->tableName.'.oidexec=exec.oid_exec'],
            ['ari_orders ord', 'ari_orders_exec.oid=ord.oid'],
            ['ari_clients cli', 'ari_orders.cid=cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        $count = Db::table($this->tableName)->join($join)->where($where)
            ->count();
        $data = Db::table($this->tableName)
            ->join($join)->where($where)
            ->paginate($num, $count, ['query' => request()->param()]);
        return $data;
    }

    public function insertAll($data){
        $result = Db::table($this->tableName)->insertAll($data);
        return $result;
    }

    /**
     * 不带分页的查询--20190515
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function joinSelect($where = array()){
        $join = array(
            ['ari_model_types model', $this->tableName.'.modelid=model.model_id'],
            ['ari_supply supply', $this->tableName.'.supid=supply.sup_id'],
            ['ari_orders_exec exec', $this->tableName.'.oidexec=exec.oid_exec'],
            ['ari_orders ord', 'ari_orders_exec.oid=ord.oid'],
            ['ari_clients cli', 'ari_orders.cid=cli.cid'],
            ['ari_states sta', 'ari_clients.sid=sta.sid']
        );
        $data = Db::table($this->tableName)->where($where)
            ->join($join)->select();
        return $data;
    }
}